import 'package:flutter/material.dart';
import 'package:flutter_app/components/text/CopyableText.dart';
import 'package:flutter_app/utils/http.dart';
import 'package:oktoast/oktoast.dart';

class Car extends StatefulWidget {

  Car({Key key, @required this.bs64}): super(key: key);
  final String bs64;

  @override
  _Car createState() => _Car();
}

class _Car extends State<Car> {

  var prsnName = Container();
  var phoneNum = Container();
  var carId = Container();
  var bottomButton = Container();
  var cardTypeContainer = Container(margin: EdgeInsets.only(top: 18, left: 30, right: 30),
      padding: EdgeInsets.only(bottom: 8, top:8),
      decoration: BoxDecoration(
          //border:Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)))
      ),
      child:Row(
        children: [
          Text("信息加载中...", style: TextStyle(fontSize: 16, color: Color.fromRGBO(102, 102, 102, 0.8))),
        ],
      ));
  var titleInfo = Container();
  var visitorTelephone = Container();
  var visitDate = Container();
  var applicantName = Container();
  var applicantTelephone = Container();

  String carNum = '';
  String pName = '';
  String telephone = '';

  @override
  void initState(){
    super.initState();
    getCarDetail();
  }

  confirmIllegalStop() async{
    ResultData info = await HttpRequest.requestPicture('/webauth/carillegalparkingmanager/saveCarIllegalParkingRecord', {
      'carNumber': this.carNum,
      'ownerName': this.pName,
      'ownerTelephone': this.telephone,
      'strBase64': widget.bs64
    });
    showToast("发送成功", duration: Duration(seconds: 1));
  }



  getCarDetail() async{
    ResultData car = await HttpRequest.requestPicture('/webauth/recognition/licensePlate', {
      'image':widget.bs64,
    });

    var cardType = car.data['cardType'];

    if(cardType == "月租A"){
      setState(() =>{
        carId = _context('车牌号', car.data['carId']),
        prsnName = _context('员工姓名', car.data['name']),
        phoneNum =  _phoneContext('联系电话',  car.data['phone']),
        bottomButton = _buttonContext('确认违停'),
        this.carNum = car.data['carId'],
        this.pName = car.data['name'],
        this.telephone = car.data['phone'],
        cardTypeContainer = Container(),
        titleInfo = _titleContext("员工车辆")
      });
    } else {
      setState(() =>{
        carId = _context('车牌号', car.data['carId']),
        _context('公司/部门',  "无"),
        cardTypeContainer = Container(),
        titleInfo = _titleContext("访客车辆"),
        if(car.data['employeeDO']!=""){
          prsnName = _context('访客姓名', car.data['employeeDO']['visitorName']),
          phoneNum =  _context('访客身份证号码',  car.data['employeeDO']['visitorCertCode']),
          visitorTelephone = _context('访客电话',  car.data['employeeDO']['visitorTelephone']),
          visitDate = _context('到访时间',  DateTime.fromMillisecondsSinceEpoch(car.data['employeeDO']['visitDate']).toString().substring(0, 19)),
          applicantName = _context('申请员工姓名',  car.data['employeeDO']['applicantName']),
          applicantTelephone = _phoneContext('申请员工电话',  car.data['employeeDO']['applicantTelephone'])
        } else {
          prsnName = _context('访客姓名', '暂无信息'),
          phoneNum =  _context('访客身份证号码',  '暂无信息'),
          visitorTelephone = _context('访客电话',  '暂无信息'),
          visitDate = _context('到访时间',  '暂无信息'),
          applicantName = _context('申请员工姓名',  '暂无信息'),
          applicantTelephone = _phoneContext('申请员工电话',  '暂无信息')
        }
      });
    }
  }

  TextStyle label = TextStyle(fontSize: 16, color: Color.fromRGBO(51, 51, 51, 1));
  TextStyle title = TextStyle(fontSize: 24, color: Colors.black87);

  _titleContext(String info){
    return Container(
      child:Row(
        children: [
          Text("此车辆为：", style: label),
          Text(info, style: title)
        ],
      ),
    );
  }

  _context(String label, String content){
    return Container(
      margin: EdgeInsets.only(top: 18, left: 30, right: 30),
      padding: EdgeInsets.only(bottom: 8, top:8),
      decoration: BoxDecoration(
          border:Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)))
      ),
      child:Row(
        children: [
          Text("$label:", style: TextStyle(fontSize: 16, color: Color.fromRGBO(102, 102, 102, 0.8))),
          Text(content, style: TextStyle(fontSize: 16, color: Colors.black87))
        ],
      ),
    );
  }

  _phoneContext(String label, String content){
    return Container(
      margin: EdgeInsets.only(top: 18, left: 30, right: 30),
      padding: EdgeInsets.only(bottom: 8, top:8),
      decoration: BoxDecoration(
          border:Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)))
      ),
      child:Row(
        children: [
          Text("$label:", style: TextStyle(fontSize: 16, color: Color.fromRGBO(102, 102, 102, 0.8))),
          CopyableText(content, style: TextStyle(fontSize: 16, color: Colors.black87, background: Paint()..color = Color(0xffe5e5e5)))
        ],
      ),
    );
  }

  _buttonContext(String label){
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(left: 30, right: 30, top: 30),
      child: (
          RaisedButton(
              color: Colors.blueAccent,
              child: Text(label, style: TextStyle(fontSize: 16.0,color: Colors.white)),
              // 设置按钮圆角
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
              onPressed: confirmIllegalStop
          )
      ),
    );
  }


  @override
  Widget build(BuildContext context) {

    // var args=ModalRoute.of(context).settings.arguments;
    return Scaffold(
      // 边距都是15
        backgroundColor: Color.fromRGBO(246, 247, 249, 1),
        // backgroundColor: Color(0xF6F7F9FF),
        appBar: AppBar(
          title: Text('车辆信息'),
          centerTitle: true,
        ),
        body: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: 30),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  cardTypeContainer,
                  titleInfo
                ],
              ),
            ),
            carId,
            prsnName,
            phoneNum,
            visitorTelephone ,
            visitDate,
            applicantName,
            applicantTelephone,
            bottomButton
          ]
        )
    );
  }
}